﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Collections.Generic;

public partial class CustomerManagement_AddCashWithdrawManagement : System.Web.UI.Page
{
    private DataSet DstDetails
    {
        get
        {
            DataSet dstDetails = new BLL.CashWithdrawDocDetails().CreateTempDataTable();
            if (ViewState["DstDetails"] != null)
            {
                dstDetails = (DataSet)ViewState["DstDetails"];
            }
            return dstDetails;
        }
        set
        {
            ViewState["DstDetails"] = value;
        }
    }

    private List<Model.CashWithdrawType> CashWithdrawTypes
    {
        get
        {
            List<Model.CashWithdrawType> cashWithdrawTypes = null;
            if (ViewState["CashWithdrawTypes"] != null)
            {
                cashWithdrawTypes = (List<Model.CashWithdrawType>)ViewState["CashWithdrawTypes"];
            }
            return cashWithdrawTypes;
        }
        set
        {
            ViewState["CashWithdrawTypes"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("300400110");
        if (!IsPostBack)
        {
            btnCancel.Attributes.Add("onclick", "if(!confirm('要取消新建结算单并返回上一页吗?'))return false;");

        }

        Session["FCode"] = "300400110";
    }

    private void BindDropType(DropDownList drop, string typeId)
    {
        for (int i = 0; i < drop.Items.Count; i++)
        {
            if (drop.Items[i].Value == typeId)
            {
                drop.SelectedIndex = i;
                break;
            }
        }
    }

    private void BindDropCashWithdrawTypes(DropDownList drop, string typeId)
    {
        if (this.CashWithdrawTypes == null)
        {
            this.CashWithdrawTypes = new BLL.CashWithdrawType().GetAllList();
        }
        ListItem item;

        for (int i = 0; i < this.CashWithdrawTypes.Count; i++)
        {
            if (this.CashWithdrawTypes[i].Type_Disabled == 0)
            {
                item = new ListItem();
                item.Text = this.CashWithdrawTypes[i].Type_Name;
                item.Value = this.CashWithdrawTypes[i].Type_ID;

                drop.Items.Add(item);
            }
        }

        for (int i = 0; i < drop.Items.Count; i++)
        {
            if (drop.Items[i].Value == typeId)
            {
                drop.SelectedIndex = i;
                break;
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("300400110", "00001"))
        {
            Share.Alert(this.Page, "无权操作"); return;
        }
        try
        {
            SetValueOfDataTable(this.DstDetails);
            if (this.DstDetails.Tables[0].Rows.Count == 0)
            {
                throw new Exception("请添加会员！");

            }
            string newId = "";

            if (new BLL.CashWithdrawDoc().InsertNew(this.DstDetails, Share.UserValue(0), out newId))
            {
                Share.InsertLog("300400110", "00001");
                Response.Write("<script>alert('新增成功！');window.location.href='CashWithdrawManagementDetail.aspx?code=" + newId + "'</script>");
            }
            else
            {
                Share.Alert(this.Page, "新增未成功，请重新添加！");
            }

        }
        catch (Exception ex)
        {
            Share.Alert(this.Page, ex.Message);
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        //Response.Redirect("LineOfCreditManagement.aspx");
    }

    protected void btnAddDetail_Click(object sender, EventArgs e)
    {
        try
        {
            string customerCode = hidUserCode.Value;
            SetValueOfDataTable(this.DstDetails);
            if (customerCode != "")
            {
                string[] customersArray = customerCode.Split('|');
                if (customersArray.Length > 0)
                {
                    string[] customer;
                    for (int i = 0; i < customersArray.Length; i++)
                    {
                        customer = customersArray[i].Split(' ');
                        if (customer.Length == 2)
                        {


                            
                            
                            this.DstDetails = new BLL.CashWithdrawDocDetails().AddDataRowToDataTable(this.DstDetails, Convert.ToString(this.DstDetails.Tables[0].Rows.Count + 1), customer[0], customer[1]);

                        }
                    }
                    dlistDetail.DataSource = this.DstDetails;
                    dlistDetail.DataBind();
                }
            }
            else
            {
                return;//Share.Alert(this.Page, "请选择会员！");
            }
            hidUserCode.Value = "";
        }
        catch (Exception ex)
        {
            Share.Alert(this.Page, ex.Message);
        }
    }

    protected void dlistDetail_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "del")
        {
            if (this.DstDetails != null)
            {
                this.DstDetails.Tables[0].Rows.RemoveAt(e.Item.ItemIndex);
                dlistDetail.DataSource = this.DstDetails;
                dlistDetail.DataBind();
            }
        }
    }

    protected void dlistDetail_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            HtmlInputHidden hidType = (HtmlInputHidden)e.Item.FindControl("hidType");
            HtmlInputHidden hidCashWithdrawType = (HtmlInputHidden)e.Item.FindControl("hidCashWithdrawType");
            DropDownList dropType = (DropDownList)e.Item.FindControl("dropType");
            DropDownList dropCashWithdrawType = (DropDownList)e.Item.FindControl("dropCashWithdrawType");
            if (dropType != null && hidType != null)
            {
                BindDropType(dropType, hidType.Value);
            }
            if (dropCashWithdrawType != null && hidCashWithdrawType != null)
            {
                BindDropCashWithdrawTypes(dropCashWithdrawType, hidCashWithdrawType.Value);
            }
        }
    }

    private void SetValueOfDataTable(DataSet dst)
    {
        Label lblDetailId;
        Label lblCustomerCode;
        Label lblCustomerName;
        DropDownList dropType;
        DropDownList dropCashWithdrawType;
        TextBox txtReceivingAccount;
        TextBox txtExpectedWithdrawAmount;
        //TextBox txtActualWithdrawAmount;
        //TextBox txtCommissionCharge;
        //TextBox txtTransferDate;
        TextBox txtDescription;

        for (int i = 0; i < dlistDetail.Items.Count; i++)
        {
            lblDetailId = (Label)dlistDetail.Items[i].FindControl("lblDetailId");
            lblCustomerCode = (Label)dlistDetail.Items[i].FindControl("lblCustomerCode");
            lblCustomerName = (Label)dlistDetail.Items[i].FindControl("lblCustomerName");
            dropType = (DropDownList)dlistDetail.Items[i].FindControl("dropType");
            dropCashWithdrawType = (DropDownList)dlistDetail.Items[i].FindControl("dropCashWithdrawType");
            txtReceivingAccount = (TextBox)dlistDetail.Items[i].FindControl("txtReceivingAccount");
            txtExpectedWithdrawAmount = (TextBox)dlistDetail.Items[i].FindControl("txtExpectedWithdrawAmount");
            //txtActualWithdrawAmount = (TextBox)dlistDetail.Items[i].FindControl("txtActualWithdrawAmount");
            //txtCommissionCharge = (TextBox)dlistDetail.Items[i].FindControl("txtCommissionCharge");
            //txtTransferDate = (TextBox)dlistDetail.Items[i].FindControl("txtTransferDate");
            txtDescription = (TextBox)dlistDetail.Items[i].FindControl("txtDescription");


            dst.Tables[0].Rows[i]["DD_ID"] = lblDetailId.Text.Trim();
            dst.Tables[0].Rows[i]["DD_CustomerID"] = lblCustomerCode.Text.Trim();
            dst.Tables[0].Rows[i]["GKMC"] = lblCustomerName.Text.Trim();
            dst.Tables[0].Rows[i]["DD_Type"] = dropType.SelectedValue;
            dst.Tables[0].Rows[i]["DD_CashWithdrawType"] = dropCashWithdrawType.SelectedValue;
            dst.Tables[0].Rows[i]["DD_ReceivingAccount"] = txtReceivingAccount.Text.Trim();
            dst.Tables[0].Rows[i]["DD_ExpectedWithdrawAmount"] = Convert.ToDecimal(txtExpectedWithdrawAmount.Text.Trim());
            //dst.Tables[0].Rows[i]["DD_ActualWithdrawAmount"] = Convert.ToDecimal(txtActualWithdrawAmount.Text.Trim());
            //dst.Tables[0].Rows[i]["DD_CommissionCharge"] = Convert.ToDecimal(txtCommissionCharge.Text.Trim());
            //dst.Tables[0].Rows[i]["DD_TransferDate"] = Convert.ToDateTime(txtTransferDate.Text.Trim());
            dst.Tables[0].Rows[i]["DD_Description"] = txtDescription.Text.Trim();
            
        }

    }
}
